package pers.jsan.litheopen.security.oauth.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.jsan.litheopen.security.cache.CacheClientManager;
import pers.jsan.litheopen.security.oauth.model.AccessToken;
import pers.jsan.litheopen.security.oauth.model.ClientAccess;
import pers.jsan.litheopen.security.oauth.service.IClientOauthService;
import pers.jsan.litheopen.utils.RandomCode;
import pers.jsan.litheopen.utils.SHA1Util;

@Service
public class ClientOauthServiceImpl implements IClientOauthService {

    @Autowired
    private CacheClientManager redisClientManager;

    @Override
    public AccessToken getAccessToken(ClientAccess clientAccess) {
        long time = System.currentTimeMillis();
        String code = RandomCode.randomString(8);
        String token = SHA1Util.encryptionSHA1(clientAccess.getClientId() + code + time);
        AccessToken accessToken = new AccessToken(token, 7200);
        clientAccess.setClientId(token);
        //缓存凭证
        redisClientManager.cacheAccessToken(accessToken, clientAccess);
        //销毁原凭证
        redisClientManager.invalidAccessToken(accessToken, clientAccess);
        return accessToken;
    }
}
